package neu.edu.vn.appmusic.ui;

import java.util.ArrayList;

import neu.edu.vn.appmusic.R;
import neu.edu.vn.appmusic.adapter.AlbumApdater;
import neu.edu.vn.appmusic.common.Constant;
import neu.edu.vn.appmusic.object.Album;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class ActivityAlbum extends ActionBarActivity {
	private ActionBar actionBar;
	private GridView gvAlbum;
	private AlbumApdater albumApdater;
	private ArrayList<Album> listAlbum;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_album);
		initGUI();
		loadContentFromServer();
		gvAlbum.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityAlbum.this, ActivityDetailAlbum.class);
				int albumId = listAlbum.get(position).getId();
				intent.putExtra("albumId", albumId);
				startActivity(intent);
			}
		});
	}
	
	/**
	 * khoi tao cac thanh phan giao dien
	 */
	private void initGUI(){
		listAlbum = new ArrayList<Album>();
		gvAlbum = (GridView) findViewById(R.id.gvAlbum);
		albumApdater = new AlbumApdater(ActivityAlbum.this, R.layout.item_grid_album, listAlbum);
		gvAlbum.setAdapter(albumApdater);
	}
	
	/**
	 * load list Album from server using volley
	 */
	public void loadContentFromServer(){
			RequestQueue queue = Volley.newRequestQueue(ActivityAlbum.this);
			StringRequest request = new StringRequest(Constant.API_GET_ALBUM, new Listener<String>() {
	
				@Override
				public void onResponse(String result) {
					try {
						JSONObject jsonObject= new JSONObject(result);
						JSONObject jsonResult = jsonObject.getJSONObject("result"); 
						
						JSONArray data = jsonResult.getJSONArray("data");
						Album album;
						for (int i = 0; i < data.length(); i++) {
							album = new Album();
							JSONObject jsonAlbum = data.getJSONObject(i);
							album.setAlbumImage(jsonAlbum.getString("albumSrc"));
							album.setAlbumName(jsonAlbum.getString("albumName"));
							album.setAlbumSinger(jsonAlbum.getString("albumSinger"));
							album.setId(jsonAlbum.getInt("id"));
							listAlbum.add(album);
						}
						if(albumApdater != null){
							albumApdater.notifyDataSetChanged();
						}
					} catch (Exception e) {
						Log.e("EXRCEPTION", e.getMessage());
					}
				}
			}, new ErrorListener() {
	
				@Override
				public void onErrorResponse(VolleyError arg0) {
					// TODO Auto-generated method stub
					Log.e("EXRCEPTION", arg0.getMessage());
				}
			});
			queue.add(request);
		}
	
}
